package com.yyw.foodie.controller.base;


import com.yyw.foodie.bo.cart.ShopCartBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.constant.BaseConstant;
import com.yyw.foodie.utils.JsonUtils;
import com.yyw.foodie.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 购物车相关前端控制器
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Slf4j
@RestController
@RequestMapping("/shopcart")
@Api(value = "购物车相关", tags = "购物车相关接口")
public class ShopCartController {

    @Autowired
    private RedisOperator redisOperator;

    @PostMapping("/add")
    @ApiOperation(value = "添加商品至购物车", notes = "添加商品至购物车")
    public Result addShopCart(@RequestParam(value = "userId", required = true) @ApiParam(value = "用户ID", required = true) String userId,
                              @RequestBody ShopCartBo shopCartBo) {
        log.info("userId: {}, 商品参数: {}", userId, shopCartBo.toString());
        // 1. 判断缓存里是否存在
        String key = BaseConstant.CACHE_CAT + userId;
        String value = redisOperator.get(key);
        List<ShopCartBo> shopCartBoList = new ArrayList<>();
        if (!StringUtils.isBlank(value)) {
            shopCartBoList = JsonUtils.jsonToList(value, ShopCartBo.class);
            // 循环判断是否存在该商品，存在则递增数量
            boolean isHaving = false;
            for (ShopCartBo cartBo : shopCartBoList) {
                if (Objects.equals(cartBo.getSpecId(), shopCartBo.getSpecId())) {
                    cartBo.setBuyCounts(cartBo.getBuyCounts() + shopCartBo.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                // 不存在直接新增
                shopCartBoList.add(shopCartBo);
            }
        }else {
            shopCartBoList.add(shopCartBo);
        }
        // 2.存放至Redis中
        String json = JsonUtils.objectToJson(shopCartBoList);
        redisOperator.set(key, json);
        return Result.ok();
    }


    @PostMapping("/del")
    @ApiOperation(value = "删除购物车", notes = "删除购物车")
    public Result delShopCart(@RequestParam(value = "userId", required = true) @ApiParam(value = "用户ID", required = true) String userId,
                              @RequestParam(value = "itemSpecId", required = true) @ApiParam(value = "skuId", required = true) String itemSpecId) {
        log.info("userId: {}, 要删除的商品ID: {}", userId, itemSpecId);
        // 1. 判断缓存里是否存在
        String key = BaseConstant.CACHE_CAT + userId;
        String value = redisOperator.get(key);
        List<ShopCartBo> shopCartBoList = new ArrayList<>();
        boolean isHaving = false;
        if (!StringUtils.isBlank(value)) {
            shopCartBoList = JsonUtils.jsonToList(value, ShopCartBo.class);
            for (ShopCartBo bo : shopCartBoList) {
                if (Objects.equals(bo.getSpecId(), itemSpecId)) {
                    shopCartBoList.remove(bo);
                    isHaving = true;
                    break;
                }
            }
        }
        // 不存在商品信息，无法删除
        if (!isHaving) {
            throw new RuntimeException("购物车为空，无法删除");
        }
        // 2.存放至Redis中
        String json = JsonUtils.objectToJson(shopCartBoList);
        redisOperator.set(key, json);
        return Result.ok();
    }
}

